package com.qzsoft.zhjd.materiel.modules.materielhome.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.materiel.modules.materielconsume.dto.MaterielConsumeProdDto;
import com.qzsoft.zhjd.materiel.modules.materielconsume.mapper.MaterielConsumeMapper;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielHomeConsumeDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielReceiveSendChartDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielReceiveSendChartListDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.dto.MaterielReceiveSendCountDto;
import com.qzsoft.zhjd.materiel.modules.materielhome.mapper.MaterielHomeMapper;
import com.qzsoft.zhjd.materiel.modules.materielhome.service.MaterielHomeService;

/**
 * @ClassName MaterielHomeServiceImpl
 * @Description 首页业务接口实现
 * @author linhuawei
 * @since 2022-04-05
 */
@Service
public class MaterielHomeServiceImpl implements MaterielHomeService {

    @Autowired
    private MaterielHomeMapper materielHomeMapper;

    @Autowired
    private MaterielConsumeMapper materielConsumeMapper;

    @Override
    public Map<String, MaterielReceiveSendCountDto> getReciveSendCount(String baseCode, WeightTypeEnum weightType) {
        List<MaterielReceiveSendCountDto> list = materielHomeMapper.getReciveSendCount(baseCode, weightType.getValue());
        Map<String, MaterielReceiveSendCountDto> result = new HashMap<>();
        if (null != list) {
            for (MaterielReceiveSendCountDto dto : list) {
                result.put(dto.getType(), dto);
            }
        }
        return result;
    }

    @Override
    public MaterielReceiveSendChartDto getReceiveSendChart(String baseCode, String type,
            WeightTypeEnum weightType) {
        List<MaterielReceiveSendChartListDto> list = materielHomeMapper.getReceiveSendChart(baseCode, type,
                weightType.getValue());
        list = ConvertDictUtils.formatDicDataList(list);

        MaterielReceiveSendChartDto dto = new MaterielReceiveSendChartDto();
        dto.setList(list);
        dto.countTotal();
        return dto;
    }

    @Override
    public Map<String, List<MaterielHomeConsumeDto>> getConsume(String baseCode, String year) {
        // 处理数据
        List<MaterielHomeConsumeDto> list = materielHomeMapper.getConsume(baseCode, year);
        List<MaterielHomeConsumeDto> asphalt = new ArrayList<MaterielHomeConsumeDto>();
        List<MaterielHomeConsumeDto> cement = new ArrayList<MaterielHomeConsumeDto>();
        List<MaterielHomeConsumeDto> gravel = new ArrayList<MaterielHomeConsumeDto>();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("baseCode", baseCode);
        MaterielConsumeProdDto proDto;
        for (MaterielHomeConsumeDto dto : list) {
            if ("db0fd12a515503c8e0be658a84c59cfd".equals(dto.getMaterielItemPkid())) {
                params.put("startDate", dto.getBeginTime());
                params.put("endDate", dto.getEndTime());
                proDto = materielConsumeMapper.queryProdDto(params);
                if (null != proDto) {
                    dto.setPlanWeigth(proDto.getAsphaltPlan());
                    dto.setProdWeigth(proDto.getAsphaltProd());
                }
                asphalt.add(dto);
            } else if ("bf9b7838aff3ceaeacb1dca0fd418c0e".equals(dto.getMaterielItemPkid())) {
                params.put("startDate", dto.getBeginTime());
                params.put("endDate", dto.getEndTime());
                proDto = materielConsumeMapper.queryProdDto(params);
                if (null != proDto) {
                    dto.setPlanWeigth(proDto.getCementPlan());
                    dto.setProdWeigth(proDto.getCementProd());
                }
                cement.add(dto);
            } else if ("26b0a3a525023a2d583d1df0aaed4dd4".equals(dto.getMaterielItemPkid())) {
                params.put("startDate", dto.getBeginTime());
                params.put("endDate", dto.getEndTime());
                proDto = materielConsumeMapper.queryProdDto(params);
                if (null != proDto) {
                    dto.setPlanWeigth(proDto.getGravelPlan());
                    dto.setProdWeigth(proDto.getGravelProd());
                }
                gravel.add(dto);
            }
        }

        // 返回结果
        Map<String, List<MaterielHomeConsumeDto>> result = new HashMap<String, List<MaterielHomeConsumeDto>>();
        result.put("asphalt", asphalt);
        result.put("cement", cement);
        result.put("gravel", gravel);
        return result;
    }

}
